/**
 * Index User State
 *
 * @date 2020-07-12
 * @author 逗号米
 */

import Vue from 'vue'
import { GetterTree, MutationTree, ActionTree, Module, Commit } from 'vuex'

import * as http from '@/http/cms/company/user'

// 接口
export interface State {
  currentPage: number,
  perPage: number,
  total: number,
  companyUsers: Array<any>,
  companyUser: any
}

// 状态
const state: State = {
  currentPage: 1,
  perPage: 20,
  total: 0,
  companyUsers: [],
  companyUser: {}
}

const getters: GetterTree<State, any> = {}

const mutations: MutationTree<State> = {
  /**
   * 分页
   *
   * @param state
   * @param current_page
   * @param page_size
   * @param total
   */
  page(state: State, {
    data_list,
    current_page,
    per_page,
    total
  }: { data_list: any, current_page: number, per_page: number, total: number }): void {
    state.companyUsers = data_list
    state.currentPage = current_page
    state.perPage = per_page
    state.total = total
  },

  /**
   * 添加
   *
   * @param state
   * @param data
   */
  store(state: State, data: any): void {
    if (state.companyUsers.length === 0) {
      state.companyUsers = [data]
    } else {
      state.companyUsers.unshift(data)
    }
  },

  /**
   * 显示
   *
   * @param state
   * @param data
   */
  show(state: State, data: any) {
    state.companyUser = data
  },

  /**
   * 更新
   *
   * @param state
   * @param index
   * @param data
   */
  update(state: State, {index, data}: { index: number, data: any }) {
    if (index !== -1) {
      Vue.set(state.companyUsers, index, data.list)
    }
    state.companyUser = data.show
  },

  /**
   * 删除
   *
   * @param state
   * @param index
   */
  destroy(state: State, index: number) {
    //Vue.set(state.companyUsers, index, data)
    state.companyUsers.splice(index, 1)
  }
}

const actions: ActionTree<State, any> = {
  /**
   * 首页
   *
   * @param commit
   * @param {Object} payload 负载
   * @return {Promise<any>}
   */
  index({commit}: { commit: Commit }, payload: any): Promise<any> {
    return  http.index(payload.params).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
  },

  /**
   * 查询
   *
   * @param commit
   * @param {Object} [payload] 负载
   * @return {Promise<any>}
   */
  query({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.query(payload.params).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
  },

  /**
   * 存储
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  store({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.store(payload.params).then((res) => {
      if (res.status === 200) {
        commit('store', res.data)
      }
      return res
    })
  },

  /**
   * 显示
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  show({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.show(payload.id).then((res) => {
      if (res.status === 200) {
        commit('show', res.data)
      }
      return res
    })
  },

  /**
   * 更新
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  update({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.update(payload.id, payload.params).then((res) => {
      if (res.status === 200 && res.data) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
  },

  /**
   * 删除
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  destroy({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.destroy(payload.id, {}).then((res) => {
      if (res.status === 200) {
        commit('destroy', payload.index)
      }
      return res
    })
  },

  /**
   * 编辑用户名
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  modifyUsername({commit}: { commit: Commit }, payload: any) {
    return http.modifyUsername(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }

      return res
    })
  },

  /**
   * 修改密码
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  modifyPassword({commit}: { commit: Commit }, payload: any) {
    return http.modifyPassword(payload.id, payload.params)
  },

  /**
   * 绑定手机号码
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  bindMobile({commit}: { commit: Commit }, payload: any) {
    return http.bindMobile(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
  },

  /**
   * 绑定邮箱
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  bindEmail({commit}: { commit: Commit }, payload: any) {
    return http.bindEmail(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
  },

  /**
   * 解除绑定
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  unBind({commit}: { commit: Commit }, payload: any) {
    return http.unBind(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
  },

  /**
   * 退出客户端
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  logoutClient({commit}: { commit: Commit }, payload: any) {
    return http.logoutClient(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
  },

  /**
   * 锁定
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  lock({commit}: { commit: Commit }, payload: any) {
    return http.lock(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
  }
}

export const companyUser: Module<State, any> = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

export default {
  companyUser
}
